home *** CD-ROM | disk | FTP | other *** search
/ Nautilus 1992 July / Nautilus-3-8 / Nautilus-3-8.bin / Tools & Utilities / Techy Stuff / Source ƒ / Task Manager Folder / TaskTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-13  |  5.9 KB  |  250 lines

  1. /*
  2.         Task Manager -- Background processing support
  3.         version 2.2
  4.  
  5.     This software source package is Copyright ⌐ 1990-91 by Michael Hecht. All Rights
  6.     Reserved. It may be freely distributed in source or object code format; however,
  7.     the source code may not be sold for profit or charged for in any way. The source
  8.     code must be distributed as a package including all H files, sample code and
  9.     projects, and documentation.
  10.  
  11.     I welcome any comments or suggestions that will help me improve or extend the
  12.     functionality of the Task Manager. You can reach me at:
  13.  
  14.         Internet:        Michael_Hecht@mac.sas.com
  15.         AppleLink:        SAS.HECHT
  16.  
  17.     Happy Tasking!
  18.  
  19.  
  20.     --Michael Hecht
  21.  */
  22.  
  23. #include "Task.h"
  24.  
  25. typedef struct {
  26.     Rect            portRect;
  27.     StringPtr        title;
  28.     StringPtr        msg1, msg2;
  29.     WindowPtr        slaveWindow;
  30. } SlaveRecord, **SlaveHandle;
  31.  
  32. /* Prototypes */
  33. static void InitToolBox( void );
  34. static OSErr NewSlave( Rect *bounds, StringPtr title, StringPtr msg1, StringPtr msg2 );
  35. static void SlaveProc( long taskRefCon );
  36. static void SlaveTermProc( long taskRefCon );
  37. static void DrawRandomMsg( WindowPtr theWindow, StringPtr msg1, StringPtr msg2 );
  38. void main( void );
  39.  
  40.  
  41. static void InitToolBox( void )
  42. {
  43.     InitGraf( &thePort );
  44.     InitFonts();
  45.     InitWindows();
  46.     InitMenus();
  47.     TEInit();
  48.     InitDialogs( 0 );
  49.     FlushEvents( everyEvent, 0 );
  50.     InitCursor();
  51. }
  52.  
  53. static OSErr NewSlave( Rect *bounds, StringPtr title, StringPtr msg1, StringPtr msg2 )
  54. {
  55.     OSErr                err;
  56.     SlaveRecord            theSlave;
  57.     SlaveHandle            theSlaveHandle;
  58.  
  59.  
  60.     /* Create a new slave; first, initialize a SlaveRecord */
  61.     theSlave.portRect = *bounds;
  62.     theSlave.title = title;
  63.     theSlave.msg1 = msg1;
  64.     theSlave.msg2 = msg2;
  65.  
  66.     /* Be sure to set this to 0, in case the term proc gets called too early! */
  67.     theSlave.slaveWindow = 0;
  68.  
  69.     /*
  70.      *    Next; convert it to a SlaveHandle, so it can sit in the heap until the
  71.      *    slave can retrieve it.
  72.      */
  73.     err = PtrToHand( &theSlave, ( Handle * )&theSlaveHandle,
  74.                         sizeof( SlaveRecord ));
  75.     if( err != noErr )
  76.         return err;
  77.  
  78.     /* Create the slave task, using the SlaveHandle as its taskRefCon */
  79.     err = NewTask( SlaveProc, SlaveTermProc, ( long )theSlaveHandle, 0 );
  80.     if( err != noErr )
  81.         DisposHandle(( Handle )theSlaveHandle );
  82.  
  83.     return err;
  84. }
  85.  
  86. static void SlaveProc( long taskRefCon )
  87. {
  88.     SlaveHandle            theSlaveHandle;
  89.     SlaveRecord            theSlave;
  90.     WindowPtr            slaveWindow;
  91.  
  92.  
  93.     /* Our refCon is really a SlaveHandle; get the record it points to */
  94.     theSlaveHandle = ( SlaveHandle )taskRefCon;
  95.     theSlave = **theSlaveHandle;
  96.  
  97.     /* Create a window for our slave */
  98.     slaveWindow = NewWindow( 0, &theSlave.portRect, theSlave.title,
  99.             TRUE, noGrowDocProc, FrontWindow(), TRUE, 0 );
  100.  
  101.     /* Store the window ptr in our SlaveHandle so the TaskTerm procedure can find it */
  102.     ( *theSlaveHandle )->slaveWindow = slaveWindow;
  103.  
  104.     /* Draw messages forever (the main task will stop us when it's time) */
  105.     for( ;; ) {
  106.  
  107.         /* Let other tasks run */
  108.         TaskYield();
  109.  
  110.         /* Draw one of our messages */
  111.         DrawRandomMsg( slaveWindow, theSlave.msg1, theSlave.msg2 );
  112.     }
  113. }
  114.  
  115. static void SlaveTermProc( long taskRefCon )
  116. {
  117.     SlaveHandle            theSlaveHandle;
  118.     WindowPtr            slaveWindow;
  119.  
  120.  
  121.     /* Our refCon is really a SlaveHandle */
  122.     theSlaveHandle = ( SlaveHandle )taskRefCon;
  123.  
  124.     /* Time to close our window (if it was ever created) */
  125.     slaveWindow = ( *theSlaveHandle )->slaveWindow;
  126.     if( slaveWindow )
  127.         CloseWindow( slaveWindow );
  128.  
  129.     /* SlaveHandle no longer needed */
  130.     DisposHandle(( Handle )theSlaveHandle );
  131. }
  132.  
  133. static void DrawRandomMsg( WindowPtr wp, StringPtr msg1, StringPtr msg2 )
  134. {
  135.     Rect            r;
  136.     RgnHandle        scrollRgn;
  137.  
  138.  
  139.     SetPort( wp );
  140.  
  141.     /* Scroll the screen */
  142.     scrollRgn = NewRgn();
  143.     r = wp->portRect;
  144.     ScrollRect( &r, 0, -16, scrollRgn );
  145.     FillRgn( scrollRgn, white );
  146.     DisposeRgn( scrollRgn );
  147.  
  148.     /* Pick one of two messages at random and draw it */
  149.     MoveTo( 5, 128 );
  150.     DrawString(( Random() & 1 ) ? msg1 : msg2 );
  151. }
  152.  
  153.  
  154. void main( void )
  155. {
  156.     OSErr                err;
  157.     WindowPtr            masterWindow, theWindow;
  158.     Rect                r;
  159.     Boolean                timeToQuit;
  160.     EventRecord            theEvent;
  161.  
  162.  
  163.     /* Initialize the ToolBox */
  164.     InitToolBox();
  165.  
  166.     /* Turn on tasking */
  167.     err = InitTasking();
  168.     if( err != noErr )
  169.         return;
  170.  
  171.     /* Make slaves */
  172.     SetRect( &r, 261, 45, 507, 176 );
  173.     err = NewSlave( &r, "\pTask 1",
  174.         "\pSee my Task 1 message?", "\pOf course you do╤I╒m Task 1!" );
  175.     if( err != noErr )
  176.         return;
  177.  
  178.     SetRect( &r, 5, 206, 251, 337 );
  179.     err = NewSlave( &r, "\pTask 2",
  180.         "\pTHIS is Task 2?", "\pY E S, it is!" );
  181.     if( err != noErr )
  182.         return;
  183.  
  184.     SetRect( &r, 261, 206, 507, 337 );
  185.     err = NewSlave( &r, "\pTask 3",
  186.         "\pThe great and mighty Task 3.", "\pSee me??? I╒m Task 3!" );
  187.     if( err != noErr )
  188.         return;
  189.  
  190.     /* Make master window */
  191.     SetRect( &r, 5, 45, 251, 176 );
  192.     masterWindow = NewWindow( 0, &r, "\pMaster Task", TRUE, noGrowDocProc,
  193.                     ( WindowPtr )-1, TRUE, 0 );
  194.  
  195.     /* A very simple event loop */
  196.     timeToQuit = FALSE;
  197.     do {
  198.         if( WaitNextEvent( everyEvent, &theEvent, 0, 0 )) {
  199.  
  200.             switch( theEvent.what ) {
  201.  
  202.             case mouseDown:
  203.                 switch( FindWindow( theEvent.where, &theWindow )) {
  204.  
  205.                 case inGoAway:
  206.                     /* Track it */
  207.                     if( !TrackGoAway( theWindow, theEvent.where ))
  208.                         break;
  209.  
  210.                     /* It's time to quit if they close the master window */
  211.                     timeToQuit = ( masterWindow == theWindow );
  212.                     break;
  213.  
  214.                 case inContent:
  215.                 case inDrag:
  216.                     /* Bring it to the front if need be */
  217.                     if( theWindow != FrontWindow())
  218.                         SelectWindow( theWindow );
  219.                     break;
  220.                 }
  221.                 break;
  222.  
  223.             case updateEvt:
  224.                 /* Clear out any update event */
  225.                 theWindow = ( WindowPtr )theEvent.message;
  226.                 BeginUpdate( theWindow );
  227.                 EndUpdate( theWindow );
  228.                 break;
  229.             }
  230.         }
  231.  
  232.         /* Allow tasks to run for five ticks */
  233.         RunTasks( 5 );
  234.  
  235.         /* Let the master task do something useful */
  236.         DrawRandomMsg( masterWindow,
  237.                         "\pObey me╤I am the Master!", "\pClose me to quit." );
  238.  
  239.     /* Continue until it's time to quit or there are no more tasks */
  240.     } while( !timeToQuit );
  241.  
  242.     /* Close the master window */
  243.     CloseWindow( masterWindow );
  244.  
  245.     /*
  246.      *    Don't forget to do this! It will dispose of all tasks,
  247.      *    thereby calling each one's TaskTerm procedure.
  248.      */
  249.     TermTasking();
  250. }